package com.atguigu.gulimaill.search.controller;

import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.common.to.es.SkuEsModel;
import com.atguigu.common.utils.R;
import com.atguigu.gulimaill.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RequestMapping("/search")
@RestController
@Slf4j
public class ElasticsearchSaveController {
    @Autowired
    ProductSaveService productSaveService;
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModelList){
        Boolean result=false;
        try {
            result=productSaveService.productStatusUp(skuEsModelList);
        } catch (IOException e) {
            log.error("商品上架错误{}:"+e);
            R.error(BizCodeEnume.PRODUCT_UP_EXECPTION.getCode(),BizCodeEnume.PRODUCT_UP_EXECPTION.getMsg());
        }
        if (result){
            return R.ok();
        }else {
            return R.error(BizCodeEnume.PRODUCT_UP_EXECPTION.getCode(),BizCodeEnume.PRODUCT_UP_EXECPTION.getMsg());
        }
    }
}
